<div ng-controller="FieldManEditC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">修改订单商品信息</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form1" class="form-validation">
                    <div class="panel panel-default">
                        <div class="container-fluid text-center bg-light lter" >
                            <div class="row">
                                <div class="col-xs-10 col-md-10">
                                    <h3 style="display: inline-block;margin: 0 auto">订单商品列表</h3>
                                </div>
                                <div class="col-xs-2 col-md-2">
                                <button type="button" ng-show="isedit" class="btn btn-sm btn-success"  ng-click="edit(item)" style="margin: 5px">
                                    编辑
                                </button>
                                <button type="button" ng-show="!isedit" class="btn btn-sm btn-success"  ng-click="edit(item)" style="margin: 5px">
                                    完成
                                </button>
                                </div>
                            </div>
                        </div>
                        <div class="container-fluid">
                            <div class="row" style="border-bottom: red solid 1px" ng-repeat="item in orderdetaildata.CommodityInfos">
                                <div class="col-xs-12 col-md-12">
                                    <div>钢厂：{{item.Commodity.Brand}}</div>
                                    <div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>
                                    <div>单价：
                                        <span ng-show="orderdetaildata.IsLockPrice==1? true:isedit">{{item.Price | number:2}} </span>
                                        <input ng-show="!isedit && orderdetaildata.IsLockPrice!=1" type="number" min="0" value="{{item.Price | number:2}}" ng-model="item.Price" required>
                                        元
                                    </div>
                                    <div>数量：
                                        <span ng-show="isedit">{{item.Count }} </span>
                                        <input ng-show="!isedit" type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" min="0" value="{{item.Count}}" ng-model="item.Count" required>
                                        件
                                    </div>
                                    <div>每吨运费：
                                        <span ng-show="isedit">{{orderdetaildata.DeliveryPrice | number:2}} </span>
                                        <input ng-show="!isedit" type="number" min="0" value="{{item.Price | number:2}}" ng-model="orderdetaildata.DeliveryPrice" required>
                                        /吨
                                    </div>
                                    <div>总质量：
                                        <span ng-show="isedit">{{item.Weight| number:3}} </span>
                                        <input ng-show="!isedit" type="number" min="0" value="{{item.Weight | number:3}}" ng-model="item.Weight" required>
                                        吨
                                    </div>
                                    <div>总运费：{{item.Weight | number:3}}*{{orderdetaildata.DeliveryPrice | number:2}}={{item.Weight*orderdetaildata.DeliveryPrice | number:2}}
                                    </div>
                                    <div>总价：{{item.Weight | number:3}}*{{item.Price | number:2}}={{item.Weight*item.Price | number:2}}</div>
                                </div>
                            </div>
                            <div class="row text-left">
                                <h4 style="display: inline-block">总价：{{orderdetaildata.Total | number:2}}</h4>
                            </div>
                            <div class="form-group pull-in clearfix">
                                <div class="col-sm-4 input-group">
                                    <span class="input-group-addon">发货时间</span>
                                    <input type="datetime-local" class="form-control" ng-model="orderdetaildata.SendTime" required>
                                </div>
                            </div>
                            <div class="row text-right">
                                <button type="button" class="btn btn-success" ng-if="typenum == 4" ng-disabled="form1.$invalid" ng-click="ordersub()">确认生成出库单</button>
                                <button type="button" class="btn btn-success" ng-if="typenum == 0" ng-disabled="form1.$invalid" ng-click="ordersub()">确认实重</button>
                                <button type="button" class="btn btn-default" ng-click="abolish()">取消修改</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
            .controller("FieldManEditC", function ($scope, $state, $http, $stateParams) {
                $scope.id = JSON.parse($stateParams.orderid);
                $scope.typenum = JSON.parse($stateParams.type);
                if(JSON.parse($stateParams.type) == 0){
//                    直发
                    $scope.myurl = '../api/FieldMan/EditPurchaseGoods'
                }else if(JSON.parse($stateParams.type) == 4){
//                    库提
                    $scope.myurl = '../api/FieldMan/EditSendInfo'
                }
                $scope.isedit = true;
                //获取计划单详细数据
                $http({
                    url:"../api/Order/Detail?id="+$scope.id,
                    method:"GET"
                })
                        .then(function (res) {
                            $scope.orderdetaildata = res.data;
                            $scope.orderdetaildata.SendTime = new Date(new Date().format("yyyy-MM-dd hh:mm:ss"));
                        })
                        .then(function (error) {
                            if(error){
                                console.log("error");
                            }
                        });
                $scope.edit = function(item){
                    $scope.isedit = !$scope.isedit;
                      $scope.changeRed();
                    $scope.sum();
                };
                //计算总价方法
                $scope.sum = function(){
                    $scope.orderdetaildata.Total = 0;
                    for (var i=0;i<$scope.orderdetaildata.CommodityInfos.length;i++)
                    {
                        $scope.orderdetaildata.Total += $scope.orderdetaildata.CommodityInfos[i].Price*$scope.orderdetaildata.CommodityInfos[i].Weight;
                    }
                };
                //保存更改
                $scope.changeRed=function(){
                    for (var i = 0; i < $('input').length; i++) {
                        console.log(i)
                        $('input').eq(i).on('change',function(){
                            $(this).parent().css('color','red')
                        })
                    }
                    
                }

                $scope.ordersub = function(){
                    console.log($scope.orderdetaildata)
                    $http.put($scope.myurl, $scope.orderdetaildata)  
                            .success(function(res){
                                if (res.State <= 0) {
                                    alert(res.Msg);    
                                } else {
                                    alert("出库成功");
                                    $state.go('app.FieldMan_FieldManDetail',{
                                        orderid:JSON.stringify($scope.id)
                                    });
                                }
                            });
                };
                //取消更改
                $scope.abolish = function(){
                    $state.go('app.FieldMan_FieldManDetail',{
                        orderid:JSON.stringify($scope.id)
                    });
                }
            })
</script>